﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using Wicresoft.Framework.Organization;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.ViewEntity;
using JDEduTrain.Framework.Entity;
using NameControlLib;

public partial class Page_SelectClassMember : BaseAuthenticationPage
{

    public string SearchDefaultText
    {
        get
        {
            return "搜索 学员姓名";
        }
    }

    public string ApplyOrganID
    {
        get
        {
            if (Request["ApplyOrganID"] != null)
                return Request["ApplyOrganID"];
            return string.Empty;
        }
    }

    public Guid ClassID
    {
        get
        {
            if (Request["ClassID"] != null)
                return new Guid(Request["ClassID"]);
            return Guid.Empty;
        }
    }



    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.ShowAdvancedSearch = false;

        this.Title = "学员列表";
        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "ThirdLevelDeptDisplayName";
            this.Order.OrderDirection = "DESC";
            this.DefaultLayout = "Simple";
            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    private void BindData()
    {
        int recordCount = 0;
        DataTable dtMember = BLTrain.GetClassMemberByApplyOrganIDs();
        DTUserPageList = dtMember.Clone();
        DTUserPageList.Clear();
        foreach (DataRow dr in dtMember.Rows)
        {
            string[] deptIDs = ApplyOrganID.Split(';');
            foreach (string deptID in deptIDs)
            {
                if (!string.IsNullOrEmpty(deptID))
                {
                    if (OrganizationHelper.IsUserInDepartment(new Guid(dr["ID"].ToString()), new Guid(deptID)))
                    {
                        DTUserPageList.ImportRow(dr);
                        recordCount++;
                        break;
                    }
                }
            }
        }
        Pager.RecordCount = recordCount;
        DTUserPageList = WebCommon.GetPagedTable(DTUserPageList, Pager.PageIndex + 1, Pager.PageSize);
        DataView dv = DTUserPageList.DefaultView;
        dv.Sort = this.Order.OrderColumn + " " + this.Order.OrderDirection;

        rptUserInfo.DataSource = dv;
        rptUserInfo.DataBind();

    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause = string.Format("{0} LIKE N'%{1}%'", UserExtend.SQLCOL_FULLNAME, tbSearch.Text);
        else
            SearchClause = "1=1";
        BindData();
    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        BindData();

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ArrayList alClass = new ArrayList();

        for (int i = 0; i < rptUserInfo.Items.Count; i++)
        {
            if ((rptUserInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptUserInfo.Items[i].ItemIndex]["ID"].ToString());
                alClass.Add(id);
            }
        }
        BLTrain.DeleteTrainClass((Guid[])alClass.ToArray(typeof(Guid)));
        BindData();
    }

    protected void lbtnAdd_Click(object sender, EventArgs e)
    {
        bool noChecked = true;
        string scrpit;
        for (int i = 0; i < rptUserInfo.Items.Count; i++)
        {
            if ((rptUserInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptUserInfo.Items[i].ItemIndex]["ID"].ToString());
                BLTrain.AddClassMemberById(id, ClassID, Guid.Empty, OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID));
                noChecked = false;
            }
        }

        scrpit = noChecked ? "alert('请选择学员！');" : "alert('添加成功！');window.opener.refreshDetail();window.close();";
        Page.ClientScript.RegisterStartupScript(GetType(), "close", scrpit, true);
    }

    protected void rptUserInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lFullName = e.Item.FindControl("lFullName") as Label;

            string id = DTUserPageList.Rows[e.Item.ItemIndex]["ID"].ToString();
            NameControlLib.NameInfo userControl = new NameInfo();
            userControl.ID = "UserNameControl_" + id;
            userControl.UserID = new Guid(id);
            userControl.ScriptPath = "../Js/NameInfo.js";
            lFullName.Controls.Add(userControl);
        }
    }

}
